<template>
  <div class="container">
    <div class="ibox-content no-border no-padding-bottom">
      <!--搜索栏-->
      <el-col class="toolbar">
        <el-form :inline="true" :model="filterForm" size="mini" label-position="top" ref="filterForm">
          <el-form-item label="id">
            <el-input v-model="filterForm.id" placeholder="id"></el-input>
          </el-form-item>
          <el-form-item label="推广类型">
            <el-select style="width: 178px" v-model="filterForm.status" placeholder="状态">
              <el-option label="全部" value=""></el-option>
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="产品">
            <el-input v-model="filterForm.market_key" placeholder="应用市场简称"></el-input>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="filterForm.market_name" placeholder="应用市场名称"></el-input>
          </el-form-item>

          <el-form-item style="margin-top: 28px">
            <el-button @click="filterClick" type="primary">查询</el-button>
            <el-button @click="resetFilterForm('filterForm')">重置</el-button>
          </el-form-item>
          <div class="hr-line-dashed"></div>
          <br>
          <el-form-item size="small">
            <el-button @click="handleAdd" type="primary" plain icon="el-icon-plus">添加推广产品</el-button>
            <el-button @click="multiplDelete" type="danger" plain icon="el-icon-delete">移除列表</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>

    <div class="ibox-content">
      <!--表格-->
      <el-table
        ref="multipleTable"
        :data="arrList"
        size="medium"
        border
        highlight-current-row
        tooltip-effect="dark"
        style="width: 100%"
        :max-height="500"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="market_key" label="推广类型"></el-table-column>
        <el-table-column prop="market_name" label="产品"></el-table-column>
        <el-table-column prop="status" label="排序" width="160"></el-table-column>
        <el-table-column label="操作" align="center" width="130" fixed="right">
          <template slot-scope="scope">
            <el-tooltip @click.native="handleDelete(scope.row)" :enterable="false" effect="dark" content="删除"
                        placement="top-start">
              <el-button type="danger" plain size="mini" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页工具条-->
      <div v-show="arrList.length>0" style="margin-top: 18px;text-align: center">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageInfo.currPage"
          :page-sizes="[20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.totalCount">
        </el-pagination>
      </div>

    </div>

    <!--    修改/添加-->
    <el-dialog width="26%" :title="editFormTitle" :visible.sync="editFormVisible" @close="closeDialog"
               :close-on-click-modal="false">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" size="small">
        <el-form-item label="应用市场简称" prop="market_key">
          <el-input v-model="editForm.market_key"></el-input>
        </el-form-item>
        <el-form-item label="应用市场名称" prop="market_name">
          <el-input v-model="editForm.market_name"></el-input>
        </el-form-item>
        <el-form-item label="是否使启用马甲包" prop="status">
          <br>
          <el-radio-group v-model="editForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click.native="closeDialog">取消</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit('editForm')">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {appMarketList, addAppMarket, delAppMarket, editAppMarket} from "../../api/appMarket";

  export default {
    name: "PageProductSetting",
    data() {
      return {
        editFormVisible: false,
        isAdd: true,
        editFormTitle: '',
        editForm: {
          status: 0
        },
        editFormRules: {
          market_key: [{required: true, message: '不能为空'}],
          market_name: [{required: true, message: '不能为空'}],
          status: [{required: true, message: '不能为空'}],
        },
        arrList: [],
        pageInfo: {
          totalPage: 0,// 总页数
          totalCount: 0,// 总条数
          pageSize: 0,// 每页条数
          currPage: 1// 当前页
        },
        filterForm: {},
        tableLoading: false,
        tableSelectData: [],
        selectOptions: {},
        chooseTime: ''
      }
    },
    created() {
      this.initData();
    },
    methods: {
      initData() {
        this.tableLoading = true;
        appMarketList({
          page: this.pageInfo.currPage,
          ...this.filterForm
        }).then(res => {
          this.tableLoading = false;
          const {list, page} = res.data;
          this.arrList = list;
          this.pageInfo = page
        }).catch(() => {
          this.tableLoading = false
        })
      },
      //分页
      handleCurrentChange(val) {
        // console.log(val);
        this.pageInfo.currPage = val;
        this.initData();
      },
      //单个删除
      handleDelete(row) {
        this.$confirm('', `确定要删除应用市场“${row.market_name}”吗`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.commonDeleteMethods(row.id)
        }).catch(() => {

        })
      },
      commonDeleteMethods(id) {
        delAppMarket({id: id}).then(res => {
          this.$message.success(res.msg);
          this.initData();
        }).catch(error => {

        })
      },
      //勾选
      handleSelectionChange(data) {
        this.tableSelectData = data;
      },
      //可以多个删除
      multiplDelete() {
        if (this.tableSelectData.length > 0) {
          let ids = [];
          this.tableSelectData.forEach((item) => {
            ids.push(item.id)
          });
          if (ids.length > 0) {
            this.$confirm('确定要删除选择的应用市场吗', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
            }).then(() => {
              this.commonDeleteMethods(ids.toString())
            }).catch(() => {

            })
          }
        } else {
          this.$message.warning('请选择要删除的应用市场')
        }
      },
      filterClick() {
        this.pageInfo.currPage = 1;
        this.initData();
      },
      //重置
      resetFilterForm(form) {
        this.$refs[form].resetFields();
        this.filterForm = {};
        this.chooseTime = '';
        this.pageInfo.currPage = 1;
        this.initData();
      },
      handleEdit(row) {
        this.editFormTitle = '编辑应用市场 ' + row.market_name;
        this.editForm = Object.assign({}, row);
        this.isAdd = false;
        this.editFormVisible = true;
      },
      closeDialog() {
        this.editFormVisible = false;
        this.resetForm();
      },
      editSubmit(form) {
        this.$refs[form].validate(valida => {
          if (valida) {
            if (this.isAdd) {
              addAppMarket({
                ...this.editForm
              }).then(res => {
                this.editFormVisible = false;
                this.$message.success(res.msg);
                this.initData();
                this.editForm = {
                  status: 0
                }
              }).catch(error => {

              })
            } else {
              editAppMarket({
                ...this.editForm
              }).then(res => {
                this.editFormVisible = false;
                this.$message.success(res.msg);
                this.initData();
                this.editForm = {
                  status: 0
                }
              }).catch(() => {

              })
            }

          }
        });
      },
      //清楚form 验证
      resetForm() {
        this.$refs['editForm'].clearValidate();
      },
      //添加管理员账号
      handleAdd() {
        this.editFormTitle = '添加应应用市场';
        this.editFormVisible = true;
        this.isAdd = true;
      },
    }
  }
</script>

<style scoped lang="scss">

</style>
